package com.jeeplus.modules.flowable.listener;


import com.jeeplus.modules.flowable.CheckAndAbnormal.Service.CheckStandardCycleListenerService;
import com.jeeplus.modules.flowable.service.FlowTaskService;
import com.jeeplus.modules.flowable.myListenerService.service.MyJianLongListenterService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component("CheckAndAbnormalListener")
public class CheckAndAbnormalListener implements ExecutionListener {

    @Autowired
    private TaskService taskService;
    @Autowired
    private FlowTaskService flowTaskService;
    @Autowired
    private CheckStandardCycleListenerService checkStandardCycleListenerService;
    @Autowired
    private MyJianLongListenterService myJianLongListenterService;


    private static CheckAndAbnormalListener checkAndAbnormalListener;
    @PostConstruct
    public void init(){
        checkAndAbnormalListener = this;
        checkAndAbnormalListener.taskService = this.taskService;
        checkAndAbnormalListener.flowTaskService = this.flowTaskService;
        checkAndAbnormalListener.checkStandardCycleListenerService = this.checkStandardCycleListenerService;
        checkAndAbnormalListener.myJianLongListenterService = this.myJianLongListenterService;
    }

    @Override
    public void notify(DelegateExecution delegateExecution) {
        String eventName = delegateExecution.getEventName();
        String eventName3 = delegateExecution.getProcessInstanceId();
        if ("start".equals(eventName)) {
//            Task task = endExencutionListener.taskService.createTaskQuery().processInstanceId(eventName3).active().singleResult();
        }else if ("end".equals(eventName)) {
            String[] tableId = checkAndAbnormalListener.myJianLongListenterService.getTableId(delegateExecution.getRootProcessInstanceId(), delegateExecution.getProcessDefinitionId());
            checkAndAbnormalListener.checkStandardCycleListenerService.insertAbnormal(tableId[1]);
        } else if ("take".equals(eventName)) {//连线监听
//            FlowElement currentFlowElement = delegateExecution.getCurrentFlowElement();
//            SubProcess subProcess = currentFlowElement.getSubProcess();
//            subProcess.getFlowElement(subProcess.getId());
        }

    }
}
